using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using JsonToXml;

namespace ThatsMeSDK.Core
{
	internal class JsonDataProvider : IDataProvider
	{
		private VBrowser fetcher;
		
		/// <summary>
		/// Creates a new instance of JsonDataProvider
		/// </summary>
		internal JsonDataProvider()
		{
			fetcher = new VBrowser(20000);
		}
		
		/// <summary>
		/// Load Json data from the TM Api
		/// </summary>
		/// <param name="contentUri">Uri to the content you want to fetch</param>
		/// <returns>XmlDocument Object with the Data</returns>
		public XmlDocument LoadData(Uri contentUri)
		{
			JsonToXmlParser parser = new JsonToXmlParser();
			XmlDocument xmldoc = new XmlDocument();
			
			string jsonData = fetcher.GetRequest(new Uri(contentUri.OriginalString + "&format=json"));
			xmldoc.LoadXml(parser.ConvertToXml(jsonData));
			
			return xmldoc;
		}
		
		/// <summary>
		/// Load Json data from the API by a POST Request
		/// </summary>
		/// <param name="contentUri">Target Uri</param>
		/// <param name="postdata">Post Data</param>
		/// <returns>XmlDocument Object with the Data</returns>
		public XmlDocument LoadDataByPost(Uri contentUri, byte[] postdata)
		{
			JsonToXmlParser parser = new JsonToXmlParser();
			XmlDocument xmldoc = new XmlDocument();
			
			string jsonData = fetcher.PostRequest(new Uri(contentUri.OriginalString + "&format=json"), postdata);
			xmldoc.LoadXml(parser.ConvertToXml(jsonData));
			
			return xmldoc;
		}
	}
}
